package com.ruoyi.learn.java.algorithm.dp;

/**
 *  2. 爬楼梯（Climbing Stairs）
 * 问题：每次可以爬 1 或 2 个台阶，问爬到第 n 阶有多少种方法。
 */
public class ClimbStairs {
    public static int climbStairs(int n) {
        if (n <= 2) return n;
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }

    // 空间优化
    public static int climbStairsOptimized(int n) {
        if (n <= 2) return n;
        int a = 1, b = 2, temp;
        for (int i = 3; i <= n; i++) {
            temp = a + b;
            a = b;
            b = temp;
        }
        return b;
    }

    public static void main(String[] args) {
        System.out.println(climbStairs(4)); // 输出: 55
    }
}
